package util.parser.wikiXML;


import java.io.File;
import java.io.IOException;


import nu.xom.Builder;
import nu.xom.Comment;
import nu.xom.DocType;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.ParsingException;
import nu.xom.ProcessingInstruction;
import nu.xom.Text;

public class XOMPrintXML {

	/**
	 * @param args
	 */
	public void parseWiki(String file_) {
		File file =new File(file_);
		Builder builder = new Builder();

		try {
			
			Document doc = builder.build(file);
			Element root = doc.getRootElement();
			listChildren(root, 0);
		}
		// indicates a well-formedness error
		catch (ParsingException ex) {
			System.out.println(file + " is not well-formed.");
			System.out.println(ex.getMessage());
		} catch (IOException ex) {
			System.out.println(ex);
		}
	}

	public void listChildren(Node current, int depth) {

		printSpaces(depth);
		String data = "";
		if (current instanceof Element) {
			Element temp = (Element) current;
			data = ": " + temp.getQualifiedName();
		} else if (current instanceof ProcessingInstruction) {
			ProcessingInstruction temp = (ProcessingInstruction) current;
			data = ": " + temp.getTarget();
		} else if (current instanceof DocType) {
			DocType temp = (DocType) current;
			data = ": " + temp.getRootElementName();
		} else if (current instanceof Text || current instanceof Comment) {
			String value = current.getValue();
			value = value.replace('\n', ' ').trim();
			if (value.length() <= 20)
				data = ": " + value;
			else
				data = ": " + current.getValue().substring(0, 17) + "...";
		}
		// Attributes are never returned by getChild()
		System.out.println(current.getClass().getName() + data);
		for (int i = 0; i < current.getChildCount(); i++) {
			listChildren(current.getChild(i), depth + 1);
		}

	}

	private void printSpaces(int n) {

		for (int i = 0; i < n; i++) {
			System.out.print(' ');
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WikiParserXOM par = new WikiParserXOM();
		par.parseWiki("F:/test/1998_International_Rules_Series_62d1.xml");

	}

}
